<?php

/*
错误类
@author:jack brown
@time:2011-12
@version:1.0.6

*/

   class error{
	 
	  /*日志文件路径,可修改*/
	  protected $ErrLogFilePath = ERR_LOG_FILE;
	  
	  /*日志传递方式,file,email,phone,database*/
	  protected $saveType = ERR_SAVE_TYPE;
	  
	  /*错误定义代码文件*/
	  protected $errCodeFile = ERR_CODE_FILE;
	  
	  /*错误报告级别*/
	  protected $errLevel = ERR_LEVEL;
	  
	  /*错误显示模板*/
	  public $errFullTpl = ERR_FULL_TPL;
	  
	  /*错误显示模板局部*/
	  public $errIncTpl  = ERR_INC_TPL;
	  
	  
	  public function __construct(){
	  
		if(DEBUG){
			error_reporting($this->errLevel);
		}else{
			error_reporting(0);
		}
		
		/*设置系统错误日志文件*/
		if(file_exists($this->ErrLogFilePath)){			
			@ini_set('error_log',$this->ErrLogFilePath);
		}else{
			
			file_put_contents($this->ErrLogFilePath,'');	
		}
		@ini_set('display_errors',1);
		$this->saveType = !preg_match('/file|email|phone/i',$this->saveType)  ? 'file' : $this->saveType;
	  }
	  
	  /*
	  显示错误信息,非系统类
      @param string $msg 错误信息
	  @param string $return 错误返回地址
	  @param boolean $inc-是否为局部显示错误
	 */
	  public function showErrorStr($msg,$return="javascript:history.back()",$inc=true){
		  
		  $err = array();
		  $err['code'] = '0';
		  $err['str'] = $msg;
		  $err['level'] = 'Waring ';
		  $err['tracer']  = '<p><strong>tracer:</strong>'.getExecTracer();
		  
		  
		  $this->errorLog($err);
		  if(DEBUG){
			 
			  if($inc == true){
				  
				  if(file_exists($this->errIncTpl)){
					  
					  $code = file_get_contents($this->errIncTpl);
					  $code = str_replace('{$error}',$err['level'].':'.$err['code'].','.$err['str'].$err['tracer'],$code);
					  $code = str_replace('{$return}',$return,$code);
					  echo $code;
				  }
				  
			  }else{
				  
				  if(file_exists($this->errFullTpl)){
					  
					  $code = file_get_contents($this->errFullTpl);
					  $code = str_replace('{$error}',$err['level'].':'.$err['code'].','.$err['str'].$err['tracer'],$code);
					  $code = str_replace('{$return}',$return,$code);
					  exit($code);
				  }
				  
			  }
			  
		  }
		  
		
		  
	  }



	  /*
	  显示错误信息,系统定义类
      @param string $msg 错误信息
	  @param string $return 错误返回地址
	  @param boolean $inc-是否为局部显示错误
	 */	  
	  public function showErrorCode($code,$return="javascript:history.back()",$inc=true){
		  
		  global $sysErrConfig;
		  
		  if($sysErrConfig == null || !is_array($sysErrConfig)){
			
				return false;  
		  }
		  
		  if($sysErrConfig[$code]!=null){
			  
			 $err = $sysErrConfig[$code]; 
			 
		  }else{
			  
			  $err = array();
			  $err['code'] = '0';
			  $err['str'] = '未知错误';
			  $err['level'] = 'Waring';
		  }
		  
		 if(DEBUG){
			  
			  if($inc == true){
				  
				  if(!file_exists($this->errIncTpl)){
					  
					  $code = file_get_contents($this->errIncTpl);
					  $code = str_replace('{$error}',$err['level'].':'.$err['code'].','.$err['str'],$code);
					  $code = str_replace('{$return}',$return,$code);
					  echo $code;
				  }
				  
			  }else{
				  
				  if(!file_exists($this->errFullTpl)){
					  
					  $code = file_get_contents($this->errFullTpl);
					  $code = str_replace('{$error}',$err['level'].':'.$err['code'].','.$err['str'],$code);
					  $code = str_replace('{$return}',$return,$code);
					  exit($code);
				  }
				  
			  }
			  
		  }
		  
		   $this->errorLog($err);
		  
	  }

		
	  /*
	  写入到日到文件
	  */
	  protected function errorLog($e){
		  
		  	  
		  if($e!=null && is_array($e)){
			  global $addr;
			 	
			  $err = $e['level'].':'.$e['code'].','.strip_tags($e['str']);
			  if(strstr($this->saveType,'file')){
				  
				 $err.=';time:'.now().'page:'.$addr['url'];
				 
				 /*将错误记录指定目录*/
				 if(!file_exists($this->ErrorLogFilePath)){
				 	 error_log($err,0);
				 }else{
					error_log($err,3,$this->ErrorLogFilePath);
				 }
				
			  }
			  
			  if(strstr($this->saveType,'email')){
				  
				  if(!class_exists('smtp')){
					  
					  include_once(WEB_CLASS_DIR.'smtp.class.php');  
				  }
				  
				  $smtp = new smtp();
				  $err.=';time:'.now().'page:'.$addr['script'];
				  $smtp->sendMail($GLOBALS['webinfor']['email'],$err,$GLOBALS['webinfor']['webname'].'网站系统提示');
				  $smtp->close();
			 
			  }
			  
			  if(strstr($this->saveType,'phone')){
				  
				  
			  }
			  
			  if($this->saveType == 'database'){
				 $err['type'] = ''; 
				sysLog($err);  
				  
			  }
		  }
		  
	  }
	   
   }


?>